package com.magicsoft.share.ui.share;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ArticleListAdapter;
import com.magicsoft.share.adapter.share.SpecialAdapter;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.share.Special;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.presenter.share.ArticleListPresenter;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.utils.MyLinearLayoutManager;
import com.magicsoft.share.utils.MyRecyclerView;
import com.magicsoft.share.view.share.IArticleListView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ArticleListFragment extends BaseFragment<IArticleListView, ArticleListPresenter> implements IArticleListView {


    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;


    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;


    private ArticleListAdapter articleListAdapter;
    private View topView;

    private SpecialAdapter specialAdapter;
    private int mType;


    public ArticleListFragment() {
        // Required empty public constructor
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_article_list;
    }

    @Override
    public ArticleListPresenter initPresenter() {
        return new ArticleListPresenter();
    }

    @Override
    protected void initData() {


    }

    @Override
    public void initView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));


        recyclerView.useDefaultLoadMore();
        //      recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNum++;
                requestData();
            }
        });


        topView = LayoutInflater.from(getContext()).inflate(R.layout.top_special, recyclerView, false);
        topView.findViewById(R.id.tv_more).setOnClickListener(new View.OnClickListener() {

            private Intent intent;

            @Override
            public void onClick(View view) {
                //更多专题
                intent = new Intent(getActivity(), SpecialListActivity.class);
                intent.putExtra("articleTypeId", mType);
                Log.d("lyf", "onClick: " + mType);
                startActivity(intent);
            }
        });
        MyRecyclerView recyclerViewSpecial = topView.findViewById(R.id.recyclerView_special);


        LinearLayoutManager linearLayoutManager = new MyLinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewSpecial.setLayoutManager(linearLayoutManager);


        specialAdapter = new SpecialAdapter(R.layout.item_special);
        specialAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Special item = specialAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), SpecialActivity.class);
                intent.putExtra("articleId",specialAdapter.getData().get(position).getId());
                intent.putExtra("special", item);
                startActivity(intent);
            }
        });
        recyclerViewSpecial.setAdapter(specialAdapter);


        articleListAdapter = new ArticleListAdapter(R.layout.item_article);
        articleListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ArticleList.ListBean item = articleListAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), ArticleActivity.class);
                intent.putExtra("aticle", item);
                intent.putExtra("articleId",item.getArticleId());
                startActivityForResult(intent, Constant.REFRESH_UI);
            }
        });
        articleListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_head) {
                    Intent intent = new Intent(getContext(), HomePageActivity.class);
                    intent.putExtra("appUserId", articleListAdapter.getItem(position).getAppUserId());
                    startActivity(intent);
                }
            }
        });

        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg2), 0, DensityUtils.dp2px(getContext(), 10), BaseQuickAdapter.HEADER_VIEW));
        articleListAdapter.addHeaderView(topView);
        recyclerView.setAdapter(articleListAdapter);


    }

    @Override
    protected void requestData() {

        mPresenter.articleList(null, pageNum, pageSize, mType, null, null);
        mPresenter.articleSpecialList(mType);
    }

    public void rrefreshData() {

        isRefresh = true;
        pageNum = 1;
        mPresenter.articleList(null, pageNum, pageSize, mType, null, null);
        mPresenter.articleSpecialList(mType);
    }


    @Override
    public void bannerListSuccess(List<com.magicsoft.share.bean.share.Banner> banners) {

    }

    @Override
    public void ArticleListSuccess(ArticleList data) {

        if (isRefresh) {
            articleListAdapter.setNewData(data.getList());
        } else {
            articleListAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }

    }

    @Override
    public void ArticleListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }

    @Override
    public void specialListSuccess(List<Special> specials) {
        specialAdapter.setNewData(specials);
        if (specials.size() == 0) {
            articleListAdapter.removeHeaderView(topView);
        }
    }

    public void setType(int type) {
        mType = type;

    }

}
